﻿// 1010final.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <limits>
#include <numeric>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <deque>

using namespace std;

class Solution01
{
public:
    Solution01(string& num_str, int length)
    {
        //num_str字符串 length整数序列长度 
        vector<int>num_vec;
        for (auto& ch : num_str)
        {
            num_vec.push_back(ch - '0');
        }
        sort(num_vec.begin(), num_vec.end());

        //正整数不超过1000
        bool is_find = false;
        int result = -1;
        for (int i = 1; i <= 1000; i++)
        {
            vector<int>curr_v;
            int count = length;
            int next = i;
            while (count--)
            {
                curr_v.push_back(next);
                next++;
            }
            vector<int>sort_v;
            for (auto&num:curr_v)
            {
                string num_str = to_string(num);
                for (auto&ch:num_str)
                {
                    sort_v.push_back(ch - '0');
                }
            }
            sort(sort_v.begin(), sort_v.end());

            if (num_vec == sort_v)
            {
                result = i;
                is_find = true;
                break;
            }
        }
        cout << result;

        //
    }
    //
};


class Solution02
{
public:
    Solution02(vector<vector<int>>& grid, vector<int>& kind_vec)
    {
        //第一行n, m
        //第二行m个整数 升序或降序 1降序 -1升序
        //n行，每行m个整数，每个商品m个属性
        //属性相同，按照下个维度排序
        //pair<int,vector<int>>p_item;

        //能不能属性对应编号？
        //属性推成一个数组，挨个sort，计算权重
        //pair<int,int>左边val, 右边index
        int row_size = grid.size();
    	int col_size = grid[0].size();
        for (int c=0; c<col_size; c++)
        {
            vector<pair<int, int>>item_v;
            for (int r=0; r<row_size; r++)
            {
                item_v.push_back(make_pair(grid[r][c], r));
            }
            if (kind_vec[c]==1)
            {
                //1降序 -1升序
                sort(item_v.begin(), item_v.end(),[](pair<int,int>&p1,pair<int,int>&p2)
                {
                    return p1.first > p2.first;
                });
            }
            else if (kind_vec[c]==-1)
            {
                sort(item_v.begin(), item_v.end(),[](pair<int, int>& p1, pair<int, int>& p2)
                {
                    return p1.first < p2.first;
                });
            }
            //权重从0到v_size，依次为v_size-0
            int item_v_size = item_v.size();
            int prev = item_v_size;
            for (int i=0; i<item_v_size; i++)
            {
                int curr_w = item_v_size - i;
                pair<int, int>curr = item_v[i];
                if (i>0 && curr.first==item_v[i-1].first)
                {
                    curr_w = prev;
                }
                index_map[curr.second].push_back(curr_w);
                prev = curr_w;
            }
	        //
        }
        //权重，序号
        vector<pair<int, int>>sort_v;

        for (auto&item:index_map)
        {
            long long weight = accumulate(item.second.begin(), item.second.end(), 0LL);
            sort_v.push_back(make_pair(weight, item.first));
        }

        sort(sort_v.begin(), sort_v.end(), [](pair<int, int>& p1, pair<int, int>& p2)
            {
                return p1.first > p2.first;
            });

        vector<vector<int>>result;
        for (auto&item:sort_v)
        {
            result.push_back(grid[item.second]);
        }

        int result_size = result.size();
        for (int r=0; r< result_size; r++)
        {
            int item_size = result[r].size();
            for (int c=0;c< item_size; c++)
            {
                cout << result[r][c];
                if (c!=item_size-1)
                {
                    cout << ' ';
                }
            }
            if (r!=result_size-1)
            {
                cout << '\n';
            }
        }
        //
    }
private:
    unordered_map<int, vector<int>>index_map;
};


/*
5 3
10 12 =C5
15 5 6
7 8 =C2+3
6 =B2-A1 =C2
7 5 3
B2:C4
 */
class Solution03
{
public:
    bool isCh(char ch)
    {
        return ch >= 'A' && ch <= 'Z';
    }
    vector<int>handleRes(string& res_str)
    {
        //B2:C4
        int col1 = -1, col2 = -1;
        int row1 = -1, row2 = -1;
        int curr_num = 0;
        for (auto& ch : res_str)
        {
            if (isCh(ch))
            {
                if (col1 == -1)
                {
                    col1 = ch - 'A';
                    continue;
                }
                col2 = ch - 'A';
            }
            else if (isdigit(ch))
            {
                curr_num = curr_num*10 + ch - '0';
            }
            else
            {
	            //碰到冒号
                row1 = curr_num;
                curr_num = 0;
            }
        	//
        }
        row2 = curr_num;
        return { row1-1,col1,row2-1,col2 };
        //
    }

    vector<int>handleStr(string&str)
    {
        bool is_compute = false;
        if (str.find('+')!=string::npos)
        {
            is_compute = true;
        }
        if (str.find('-')!=string::npos)
        {
            is_compute = true;
        }
        //2+2+1
	    // =B2-A1 第1个行列 第2个行列 第三个符号
        //3+C2 那么肯定只更了
        int col1 = -1, col2 = -1;
        int row1 = -1, row2 = -1;

        //第一次col为空，数字只会更新到row
        bool is_prev_num = false;

        int sign = 1;
        int curr_num = 0;
        if (is_compute)
        {
            for (auto&ch:str)
            {
                if (isCh(ch))
                {
                    if (col1 == -1)
                    {
                        if (is_prev_num)
                        {
                            col2 = ch - 'A';
                        }
                        else
                        {
                            col1 = ch - 'A';
                        }
                        continue;
                    }
                    col2 = ch - 'A';
                }
                else if (isdigit(ch))
                {
                    curr_num = curr_num * 10 + ch - '0';
                }
                else if (ch=='-')
                {
                    sign = -1;
                    row1 = curr_num;
                    curr_num = 0;
                    if (col1 == -1) is_prev_num = true;
                }
                else if (ch=='+')
                {
                    sign = 1;
                    row1 = curr_num;
                    curr_num = 0;
                    if (col1 == -1) is_prev_num = true;
                }
                //
            }
            row2 = curr_num;

            return { row1-1, col1, row2-1, col2, sign };
	        //
        }

        
        for (auto& ch : str)
        {
            if (isCh(ch))
            {
                if (col1 == -1)
                {
                    col1 = ch - 'A';
                }
            }
            else if (isdigit(ch))
            {
                curr_num = curr_num * 10 + ch - '0';
            }
        }
        
        row1 = curr_num;
        return { row1-1, col1 };
        //
    }

    bool isNumStr(string&str)
    {
        bool res = true;
        for (auto&ch:str)
        {
	        if (!isdigit(ch))
	        {
                res = false;
                break;
	        }
        }
        return res;
    }

	Solution03(vector<vector<string>>& grid, string& res_str)
    {
        //表格行号1-20 列号A-Z
        int row_size = grid.size(), col_size = grid[0].size();
        vector<vector<int>>real_grid(row_size, vector<int>(col_size));

        for (int r=0; r<row_size; r++)
        {
            for (int c=0; c<col_size; c++)
            {
                string curr = grid[r][c];
                if (isNumStr(curr))
                {
                    real_grid[r][c] = stoi(curr);
                    continue;
                }
                vector<int>res = handleStr(curr);

                //row1 col1 row2 col2 sign
                if (res.size()==5)
                {
                    int row1 = res[0], col1 = res[1];
                    int row2 = res[2], col2 = res[3];
                    int sign = res[4];

                    string num_str_1;
                    if (col1!=-1)
                    {
                        num_str_1 = grid[row1][col1];
                    }
                    else
                    {
                        num_str_1 = to_string(row1+1);
                    }
                    
                    int num1 = stoi(num_str_1);
                    string num_str_2;
                    if (col2!=-1)
                    {
                        num_str_2 = grid[row2][col2];
                    }
                    else
                    {
                        num_str_2 = to_string(row2+1);
                    }

                    int num2 = stoi(num_str_2);

                    int num_res = num1 + num2 * sign;
                    real_grid[r][c] = num_res;
	                //
                }
                if (res.size()==2)
                {
                    int row1 = res[0], col1 = res[1];
                    string num_str = grid[row1][col1];
                    real_grid[r][c] = stoi(num_str);
                }
				//
            }
            //
        }


        vector<int>res_v = handleRes(res_str);

        int row_start = res_v[0], col_start = res_v[1];
        int row_end = res_v[2], col_end = res_v[3];

        int result = 0;
        for (int r=row_start; r<=row_end; r++)
        {
            for (int c=col_start;c<=col_end;c++)
            {
                result += real_grid[r][c];
            }
	        //
        }
        cout << result;
        //
    }
};

int main()
{
	{
        int row_size, col_size;
        cin >> row_size >> col_size;

        vector<vector<string>>grid(row_size, vector<string>(col_size));

        for (int r=0;r<row_size; r++)
        {
	        for (int c=0; c<col_size; c++)
	        {
                cin >> grid[r][c];
	        }
        }
        string res_str;
        cin >> res_str;

        Solution03 solu(grid, res_str);

        return 0;
	}
	{
        int N, M;
        cin >> N >> M;

        int row_size = N, col_size = M;
        vector<int>kind_vec(col_size);
        for (int c = 0; c < col_size; c++)
        {
            cin >> kind_vec[c];
        }

        vector<vector<int>>grid(row_size, vector<int>(col_size));
        for (int r = 0; r < row_size; r++)
        {
            for (int c = 0; c < col_size; c++)
            {
                cin >> grid[r][c];
            }
        }
        Solution02 solu(grid, kind_vec);

        return 0;
	}
    //19801211 5
    string num_str;
    cin >> num_str;
    int length;
    cin >> length;

    Solution01 solu(num_str, length);
    return 0;
    //
    
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
